package com.ifinc.common.socket.netty.server;

/**
 * Created by IntelliJ IDEA.
 * User: luotao
 * Date: 12-5-30
 * Time: 00:46
 */

import com.ifinc.common.socket.server.MessageDecoder;
import com.ifinc.common.socket.server.MessageEncoder;
import com.ifinc.common.socket.simple_netty_server.SimpleMessageHandler;
import org.jboss.netty.channel.ChannelHandler;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.handler.logging.LoggingHandler;
import org.jboss.netty.logging.InternalLoggerFactory;
import org.jboss.netty.logging.Log4JLoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


public class ServerMessageFactory implements ChannelPipelineFactory {

    /**
     * 消息解码器
     */
    private ChannelHandler messageDecoder = new MessageDecoder(Integer.MAX_VALUE, 0, 4, 0, 4);

    /**
     * 消息编码器
     */
    private ChannelHandler messageEncoder = new MessageEncoder(4);

    /**
     * 消息分发的字节流处理器
     */
    private ServerMessageHandler serverMessageHandler;

    @Autowired
    public void setMessageHandler(ServerMessageHandler messageHandler) {
        this.serverMessageHandler = messageHandler;
    }

    public ChannelPipeline getPipeline() throws Exception {

        ChannelPipeline cp = Channels.pipeline();
        return addMessageHandlersTo(cp);
    }

    /**
     * 给ChannelPipeline对象增加消息协议的字节流处理器集合
     *
     * @param cp
     * @return
     */
    public ChannelPipeline addMessageHandlersTo(ChannelPipeline cp) {

        InternalLoggerFactory.setDefaultFactory(new Log4JLoggerFactory());
        cp.addLast("logging", new LoggingHandler());

		cp.addLast("messageDecoder", messageDecoder);
		cp.addLast("messageEncoder", messageEncoder);

		cp.addLast("messageHandler", serverMessageHandler);


//        cp.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
//        cp.addLast("messageDecoder", new StringDecoder());
//        cp.addLast("messageEncoder", new StringEncoder());
//
//        // and then business logic.
//        cp.addLast("messageHandler", messageHandler);


        return cp;
    }

}